package com.heyqing.medium.day8;

import java.util.*;

/**
 * ClassName:CountOfSubstrings
 * Package:com.heyqing.medium.day8
 * Description:
 *
 * @Date:2024/10/10
 * @Author:Heyqing
 */
public class CountOfSubstrings {
    public int countOfSubstrings(String word, int k) {
        List<Character> targetList = generateTarget();
        Set<Character> currentSet = new HashSet<>(5);
        int start = 0, consonant = 0, result = 0;
        for (int i = 0; word.length() - start >= 5 + k; i++) {
            if (i >= word.length()) {
                i = start;
                start++;
                currentSet.clear();
                consonant = 0;
            }
            char w = word.charAt(i);
            if (targetList.contains(w)) {
                currentSet.add(w);
            } else {
                consonant++;
            }
            if (currentSet.size() == 5) {
                if (consonant == k) {
                    result++;
//                    i = start;
//                    start++;
//                    currentSet.clear();
//                    consonant = 0;
                } else if (consonant > k) {
                    i = start;
                    start++;
                    currentSet.clear();
                    consonant = 0;
                }
            }
        }
        currentSet.clear();
        consonant = 0;
        for (int i = 0; i < word.length(); i++) {
            if (consonant > k) break;
            if (targetList.contains(word.charAt(i))){
                currentSet.add(word.charAt(i));
            }else {
                consonant++;
            }
        }
//        if (currentSet.size() == 5 && consonant == k) result++;
//        if (result != 0 && word.length() == 5 + k) result--;
        return result;
    }

    private List<Character> generateTarget() {
        List<Character> targetList = new ArrayList<>(5);
        targetList.add('a');
        targetList.add('e');
        targetList.add('i');
        targetList.add('o');
        targetList.add('u');
        return targetList;
    }
}
